package com.bishe.supermarket.service;

import com.bishe.supermarket.mapper.CommodityMapper;
import com.bishe.supermarket.mapper.ProfitMapper;
import com.bishe.supermarket.mapper.SaveMapper;
import com.bishe.supermarket.pojo.Commodity;
import com.bishe.supermarket.pojo.Profit;
import com.bishe.supermarket.pojo.Save;
import com.bishe.supermarket.pojo.vo.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleService {

      @Autowired
    ProfitMapper profitMapper;

      @Autowired
    SaveMapper saveMapper;
      @Autowired
    CommodityMapper commodityMapper;
    //插入销售记录
    //profit表中插入

    public Status addSale(String comName,int comNum,int comProfit){
        Status status = new Status();

//
//          Save save = saveMapper.select(comName);
////        System.out.println(num);
//        if (save==null){
//            status.setMsg("false");
//            status.setMsg("仓库中没有该商品，无法添加订单！");
//            return status;
//        }
//        if (save.getCommodityNum()<=comNum){
//            status.setMsg("false");
//            status.setMsg("仓库中货量不足，无法添加订单！");
//            return status;
//        }
//        //如果数量还有，修改数量
//        //如果数量没有 从数据库中仓库里面删除订单中的商品
//        if (comNum-save.getCommodityNum()>0){
//            //把数量改了
//            Save save1 = new Save();
//            save1.setCommodityNum(comNum-save.getCommodityNum());
//            save1.setCommodityName(comName);
//            saveMapper.update(save1);
//
//        }
//        if(comNum-save.getCommodityNum()==0){
//            saveMapper.delete(comName);
//        }

        if(comNum<=0){
            status.setMsg("请输入合法数量！");
            status.setCode(true);
            return status;
        }
        Commodity commodity = commodityMapper.select(comName);
        if (commodity==null){
            status.setMsg("false");
            status.setMsg("货架上没有该商品，无法添加订单！");
            return status;
        }
        if (commodity.getCommodityNum()<comNum){
            status.setMsg("false");
            status.setMsg("货架上货量不足，无法添加订单！");
            return status;
        }

        //添加订单之后，货架上面就要减少
        if(commodity.getCommodityNum()-comNum==0){
            //直接删除货架上面的该商品
            commodityMapper.delete(comName);
        }else {

            Commodity commodity1 = new Commodity();
            commodity1.setCommodityName(comName);
            commodity1.setCommodityNum(commodity.getCommodityNum()-comNum);

            commodityMapper.updateNum(commodity1);

        }


        Profit profit = new Profit();

        profit.setCommodityName(comName);
        profit.setCommoditySalenum(comNum);
        profit.setCommodityProfit(comProfit);
        if (comProfit>0){
            profit.setIsGain("是");
        }else {
            profit.setIsGain("否");
        }



        profitMapper.insert(profit);

      status.setCode(true);
      status.setMsg("添加成功！");

      return status;
    }

    //退货处理，从profit中判断有无订单号

    public Status backSale(int orderId){
        Status status = new Status();

        //判断id是否在profit中
       List<Integer> list = profitMapper.selectId();

        if (!list.contains(orderId)){
            status.setCode(false);
            status.setMsg("无该订单号！");
             return status;
        }

        profitMapper.delete(orderId);

        status.setCode(true);
        status.setMsg("退货成功！");



       return status;

    }


}
